package client.libmessenger;
import java.io.*;
import common.*;

public class ClientThread implements Runnable
{
	public SocketWrapper clientSocket = null;
	
	public void run()
	{
		try
		{
			String line = clientSocket.readLine();
			
			if(line.equals("MESSAGE"))
			{
				String from = clientSocket.readLine();
				int msgSize = Integer.valueOf(clientSocket.readLine()).intValue();
				//char[] message = new char[msgSize];
				
				//clientSocket.in.read(message);				
				//clientSocket.in.read(message, 0, msgSize);
				/*
				char[] msgBytes = new char[msgSize];				
				int bytesRead = clientSocket.in.read(msgBytes);
				String message = "";
				
				for(int i=0; i<bytesRead; ++i)
					message = message + msgBytes[i];
				*/
				
				String message = "";
				while(message.length() < msgSize)
					message = message + clientSocket.readLine()+"\n";
				
				message = message.trim();
				
				System.out.println("Received message size is #"+msgSize+ " bytes");
				System.out.println("Actual message size is #"+message.length()+ " bytes");
				
				LibMessenger.instance.listener.onReceiveMessage(from, message);								
			} // IF
			
			else if(line.equals("STATUS CHANGE"))
			{
				String username = clientSocket.readLine();
				int status = Integer.valueOf(clientSocket.readLine()).intValue();
				LibMessenger.instance.listener.onClientChangeStatus(username, status);
				
				clientSocket.println("+OK new status received");
			}
			
			// someone is connecting to me and asking for permission to send a file
			else if(line.equals("FILE TRANSFER PERMISSION"))
			{
				String username = clientSocket.readLine();
				String filename = clientSocket.readLine();
				String filesize = clientSocket.readLine();
				
				LibMessenger.instance.listener.onFileTransferOffer(username, filename, filesize);
			}
			
			// some is connecting to me and asking to send a file
			else if(line.equals("FILE TRANSFER"))
			{
				String filename = clientSocket.readLine();
				String username = clientSocket.readLine();
				File file = new File(filename);
				int fileSize = 0;
				int bytesSent = 0;
				
				if (file.exists())
				{
					clientSocket.println("+OK file size and contents follow");
					fileSize = (int) file.length();					
					clientSocket.println(String.valueOf(fileSize));
					
					LibMessenger.instance.listener.onFileSendStart(username, filename, fileSize);
					//LibMessenger.instance.listener.onFileSendProgress(username, filename, fileSize, bytesSent);
					
					FileInputStream in = new FileInputStream(filename);
					for(int i=1; i<=fileSize; ++i)
					{
						clientSocket.socket.getOutputStream().write(in.read());
						 LibMessenger.instance.listener.onFileSendProgress(username, filename, fileSize, bytesSent);
					    bytesSent++;						
					}
					
					LibMessenger.instance.listener.onFileSendProgress(username, filename, fileSize, bytesSent);
					in.close();
					// start sending the file, byte by byte
					/*
					FileReader reader = new FileReader (file);
					while (reader.ready())
					{
					    int aChar = reader.read();
					    clientSocket.socket.getOutputStream().write(aChar);
					    LibMessenger.instance.listener.onFileSendProgress(username, filename, fileSize, bytesSent);
					    bytesSent++;
					} // WHILE

					reader.close();
					*/
				} // IF
				else
				{
					clientSocket.println("-ERR file not found");
				}
				
			}
			
			else if(line.equals("GAME START"))
			{
				String username = clientSocket.readLine();
				String gameName = clientSocket.readLine();
				
				LibMessenger.instance.listener.onGameInitiation(username, gameName);
			}
			
			else if(line.equals("GAME MOVE"))
			{
				String username = clientSocket.readLine();
				String gameName = clientSocket.readLine();
				String move = clientSocket.readLine();
				
				LibMessenger.instance.listener.onGameMove(username, gameName, move);
			}
			
			else
			{
				clientSocket.println("-ERR unrecognized command");
			}
			
			clientSocket.close();
		} // TRY
		catch(Exception e)
		{
			System.err.println("In ClientThread::run():");
			e.printStackTrace();
			System.exit(-1);			
		} // CATCH		
	}

}
